package com.litchi.activity;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.FindListener;

import com.lib.uil.ToastUtils;
import com.litchi.R;
import com.litchi.app.MyApplication;
import com.litchi.bean.Order;
import com.litchi.bean.OrderGoods;
import com.litchi.bean.UserBean;
import com.litchi.utils.Constants;

public class OrdersActivity extends Activity implements OnClickListener {

	private UserBean mUserBean;
	private Context mContext;
	private Handler orderHandler;
	private List<OrderGoods> mOrderGoods;
	List<Order> orders;
	private TextView noOrderText;
	private ListView mListView;
	private MyApplication mApplication;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_orders);
		findViewById(R.id.img_back).setOnClickListener(this);
		noOrderText = (TextView) findViewById(R.id.text_no_order);
		mListView=(ListView)findViewById(R.id.list_order);
		mApplication=(MyApplication)getApplicationContext();

		mContext = this;
		
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		initData();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.img_back:
			finish();
			break;

		default:
			break;
		}
	}

	public void initData() {
		mOrderGoods = new ArrayList<OrderGoods>();
		orders = new ArrayList<Order>();
		orderHandler = new Handler() {
			@SuppressWarnings("unchecked")
			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);

				switch (msg.what) {
				case Constants.BMOB_SUCCESS_CODE:
					List<String> orderIdList = new ArrayList<String>();

					orders = (List<Order>) msg.obj;

					if (orders.size() > 0) {
						for (Order order : orders) {
							orderIdList.add(order.getOrderId());
						}

						if (orderIdList.size() > 0) {
							BmobQuery<OrderGoods> query = new BmobQuery<OrderGoods>();
							query.addWhereContainedIn("orderId", orderIdList);
							query.findObjects(mContext,
									new FindListener<OrderGoods>() {

										@Override
										public void onError(int arg0,
												String arg1) {
											Message msg = new Message();
											msg.what = Constants.BMOB_ORDER_GOODS_FAIL_CODE;
											msg.obj = arg1;
											sendMessage(msg);
										}

										@Override
										public void onSuccess(
												List<OrderGoods> arg0) {
											 
											 
											Message msg = new Message();
											msg.what = Constants.BMOB_ORDER_GOODS_SUCCESS_CODE;
											msg.obj = arg0;
											sendMessage(msg);

										}

									});

						}

					} else {
						noOrderText.setVisibility(View.VISIBLE);
					}

					break;
				case Constants.BMOB_FAIL_CODE:
					ToastUtils.showToast(mContext, (String) msg.obj);
					break;

				case Constants.BMOB_ORDER_GOODS_SUCCESS_CODE:
					noOrderText
					.setVisibility(View.GONE);
					
					if (null!=orders) {
						mOrderGoods=(List<OrderGoods>) msg.obj;
						
						mListView.setAdapter(new MyAdapter());
						
					}
					
					
					break;

				case Constants.BMOB_ORDER_GOODS_FAIL_CODE:
					ToastUtils.showToast(mContext, (String) msg.obj);
					break;
				default:
					break;
				}

			}
		};

		mUserBean = BmobUser.getCurrentUser(mContext, UserBean.class);

		BmobQuery<Order> orderQuery = new BmobQuery<Order>();
		orderQuery.addWhereEqualTo("userId", mUserBean.getObjectId());
		orderQuery.findObjects(mContext, new FindListener<Order>() {

			@Override
			public void onError(int arg0, String arg1) {
				Message msg = new Message();
				msg.what = Constants.BMOB_FAIL_CODE;
				msg.obj = arg1;
				orderHandler.sendMessage(msg);
			}

			@Override
			public void onSuccess(List<Order> arg0) {

				Message msg = new Message();
				msg.obj = arg0;
				msg.what = Constants.BMOB_SUCCESS_CODE;
				orderHandler.sendMessage(msg);
			}

		});
	}

	public class MyAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return orders.size();
		}

		@Override
		public Object getItem(int arg0) {
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder mViewHolder;
			final Order order=orders.get(position);
			
			final ArrayList<String> selectedList = new ArrayList<String>();//保存订单商品显示图
			final ArrayList<String> goodsList = new ArrayList<String>();

			if (null==convertView) {
				mViewHolder=new ViewHolder();
				convertView=LayoutInflater.from(mContext).inflate(R.layout.item_order,null);
				mViewHolder.orderIdTv=(TextView)convertView.findViewById(R.id.text_order_id);
				mViewHolder.orderStatusTv=(TextView)convertView.findViewById(R.id.text_order_status);
				mViewHolder.orderPriceTv=(TextView)convertView.findViewById(R.id.text_order_price);
				mViewHolder.orderPayTv=(TextView)convertView.findViewById(R.id.text_order_gopay);
				mViewHolder.mImgLayout=(LinearLayout)convertView.findViewById(R.id.img_order_goods);
				mViewHolder.orderAddressName=(TextView)convertView.findViewById(R.id.text_order_address_name);
				mViewHolder.orderAddressPhone=(TextView)convertView.findViewById(R.id.text_order_address_phone);
				mViewHolder.orderAddress=(TextView)convertView.findViewById(R.id.text_order_address);
				
				for (int i = 0; i < mOrderGoods.size(); i++) {
					if (order.getOrderId().equals(mOrderGoods.get(i).getOrderId())) {
						selectedList.add(mOrderGoods.get(i).getGoodsImg());
						goodsList.add(mOrderGoods.get(i).getGoodsId() + ":"
								+ mOrderGoods.get(i).getGoodsName() + ":"
								+ mOrderGoods.get(i).getGoodsImg() + ":"
								+ mOrderGoods.get(i).getGoodsPrice() + ":"
								+ mOrderGoods.get(i).getGoodsCount());
					ImageView img=new ImageView(mContext);
			        mApplication.imgLoader.displayImage(Constants.HTTP_FILE+mOrderGoods.get(i).getGoodsImg(), img );
			        mViewHolder.mImgLayout.addView(img);
					}
				}
				
				convertView.setTag(mViewHolder);
			}else {
				mViewHolder=(ViewHolder)convertView.getTag();
				
			}
			
		
			
			final String price=order.getPrice();
			mViewHolder.orderIdTv.setText("订单ID:"+order.getOrderId());
			mViewHolder.orderStatusTv.setText(order.getStatus());
			mViewHolder.orderPriceTv.setText(price);
			mViewHolder.orderAddressName.setText(order.getAddressName());
			mViewHolder.orderAddressPhone.setText(order.getAddressPhone());
			mViewHolder.orderAddress.setText(order.getAddress());
			
			if (order.getStatus().equals(Constants.ORDER_STATUS_UNPAID_KEY)) {
				mViewHolder.orderPayTv.setVisibility(View.VISIBLE);
			}else {
				mViewHolder.orderPayTv.setVisibility(View.GONE);
			}
			
			
			
			mViewHolder.orderPayTv.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(mContext, ConfirmOrderActivity.class);
					intent.putStringArrayListExtra(Constants.CART_CONFIRM_KEY, selectedList);
					intent.putStringArrayListExtra(Constants.CART_CONFIRM_GOODS_KEY,
							goodsList);
					intent.putExtra(Constants.CART_PRICE_KEY, price);
					intent.putExtra(Constants.NEW_ORDER,false);
					intent.putExtra(Constants.ORDER_ID,order.getOrderId());

					startActivity(intent);
				}
			});
			
			 
			return convertView;
		}
		
		class ViewHolder{
			
			TextView orderIdTv,orderStatusTv,orderPriceTv,orderPayTv;
			TextView orderAddressName,orderAddressPhone,orderAddress;

			LinearLayout mImgLayout;
		}

	}

}
